package cst.neu.yuejian_31.model;

import cst.neu.yuejian_31.bean.UserInfoBean;
import cst.neu.yuejian_31.iface.UserInfoIface;
import cst.neu.yuejian_31.iface.UserInfoListener;
import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.service.UserInfoService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xxe on 2017/10/13.
 */

public class UserInfoModel implements UserInfoIface {
    private Retrofit retrofit;
    public UserInfoModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getUserInfo(String user_id,final UserInfoListener userInfoListener) {
        UserInfoService userInfoService=retrofit.create(UserInfoService.class);
        Call<UserInfoBean>call
                =userInfoService.getUserInfo(user_id);
        call.enqueue(new Callback<UserInfoBean>() {
            @Override
            public void onResponse(Call<UserInfoBean> call, Response<UserInfoBean> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    userInfoListener.onResponse(response.body());
                }
                else {
                    userInfoListener.onFail("FAIL");
                }
            }

            @Override
            public void onFailure(Call<UserInfoBean> call, Throwable t) {

                userInfoListener.onFail(t.toString());
            }
        });
    }
}
